﻿using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Models.Wishire.entity;
using System.Reflection;
/*
//插入一个answer_st
public void answerInsert(answer_st answer_St)
//根据ID删除
public void answerDelete(int ID)
//根据ID更新数据
public void answerUpdate(answer_st answer_St, int ID)
//根据ID查询 只查得到一个数据
public answer_st answerFind(int ID)
//根据planID查询 返回一个数组
public List<answer_st> answerFind_planId(int ID)
//根据城市名称查询 返回一个数组
public List<answer_st> answerFind_answerCity(string Name)
//根据景点名称查询 返回一个数组
public List<answer_st> answerFind_answerName(string Name)
*/
namespace Models.Wishire.Operator
{
    class opdataanswer
    {
        //插入一个answer_st
        public void answerInsert(answer answer)
        {
            /*private string name;
            private double lng;
            private double lat;
            private int answerId;
            private int planId;
            private int index;
            private int ifdeleted;*/
            string nameStr = answer.GetName();
            string lngStr = answer.GetLng().ToString();
            string latStr = answer.GetLat().ToString();
            string answerIdStr = answer.GetAnswerId().ToString();
            string planIdStr = answer.GetPlanId().ToString();
            string indexStr = answer.GetIndex().ToString();
            string ifdeletedStr = answer.GetIfDeleted().ToString();
            //string temp = "insert into plan_st (planId,planName,accountId,cityName,startTime,endTime,ifDeleted,setTime,empty,hotelName,hotelLat,hotelLng) ";
            string temp = "insert into answer ";
            string value = $"value('{answerIdStr}','{planIdStr}','{nameStr}','{indexStr}','{ifdeletedStr}','{latStr}','{lngStr}'";
            value += ")";
            string query = temp + value;
            try
            {
                MysqlOp mysqlOp = new MysqlOp();
                mysqlOp.Mysqlop(query);
                Console.WriteLine("增加成功");

            }
            catch (Exception ex)
            {
                Console.WriteLine("增加失败");
            }
        }
        //根据ID删除
        public void answerDeleteall(int planId)
        {
            string temp = "delete from  answer  where ";
            string value = $"planId={planId}";
            string query = temp + value;
            try
            {
                MysqlOp mysqlOp = new MysqlOp();
                mysqlOp.Mysqlop(query);
                Console.WriteLine("删除成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("删除失败");
            }
        }
        public void deleteans(int Id)
        {
            //string temp = "insert into plan_st (planId,planName,accountId,cityName,startTime,endTime,ifDeleted,setTime,empty,hotelName,hotelLat,hotelLng) ";

            string temp = "delete from  answer  where ";
            string value = $"answerId={Id}";
            string query = temp + value;
            try
            {
                MysqlOp mysqlOp = new MysqlOp();
                mysqlOp.Mysqlop(query);
                Console.WriteLine("删除成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("删除失败");
            }
        }
        public void answerDelete(int planId,int index)
        {
            //string temp = "insert into plan_st (planId,planName,accountId,cityName,startTime,endTime,ifDeleted,setTime,empty,hotelName,hotelLat,hotelLng) ";
            
            string temp = "delete from  answer  where ";
            string value = $"planId={planId} and answerindex = {index}";
            string query = temp + value;
            try
            {
                MysqlOp mysqlOp = new MysqlOp();
                mysqlOp.Mysqlop(query);
                Console.WriteLine("删除成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("删除失败");
            }
        }
        public void answerUpdate(answer ans)
        {
            try
            {
                answer tempans = answerFind_answerId(ans.GetAnswerId());
                tempans = ans;
                deleteans(ans.GetPlanId());
                answerInsert(ans);
                Console.WriteLine("修改成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("修改失败");
            }

        }

        public void swapanswer(int planId, int index1,int index2)
        {
            answer answer1 = answerFind(planId, index1);
            answer answer2 = answerFind(planId, index2);
            answerDelete(planId, index1);
            answerDelete(planId, index2);
            int indexa = answer1.GetIndex();
            int indexb = answer2.GetIndex();
            int index = indexa;
            indexa = indexb;
            indexb = index;
            answer1.SetIndex(indexa);
            answer2.SetIndex(indexb);
            answerInsert(answer1);
            answerInsert(answer2);
            Console.WriteLine("交换成功");
        }
        //根据ID查询 只查得到一个数据
        public answer answerFind(int planId,int index)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  answer  where planId={planId} and answerindex = {index} ";
            var ans = mysqlOp.Mysql_answer_Query(temp);
            answer answer = null;
            if (ans.Count > 0) answer = ans[0];
            return answer;
        }
        public List<answer> answerFindall()
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  answer";
            var ans = mysqlOp.Mysql_answer_Query(temp);
            return ans;
        }
        //根据planID查询 返回一个数组
        public List<answer> answerFind_planId(int ID)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  answer  where planId={ID} ";
            var ans = mysqlOp.Mysql_answer_Query(temp);
            return ans;
        }
        public List<plan_st> answerFind_accountId(int ID)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  plan_st  where accountId={ID} ";
            var ans = mysqlOp.Mysql_planst_Query(temp);
            return ans;
        }//AAAAA
        public answer answerFind_answerId(int ID)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  answer  where answerId={ID} ";
            var ans = mysqlOp.Mysql_answer_Query(temp);
            return ans[0];
        }
    }
}
